package com.sky.task;

import com.sky.mapper.admin.ReportMapper;
import com.sky.vo.UserCollectVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

@Component
@Slf4j
public class UserCollectTask {

    @Autowired
    private ReportMapper reportMapper;

    /**
     * 定时查询数据库统计用户
     */
    @Scheduled(cron = "1 0 0 * * ?")
    public void insertUserCollect(){
        log.info("开始查询每日用户数量及每日新增用户数量");
        Integer totalUser = reportMapper.userTotalCollect(LocalDate.now().minusDays(1L));
        log.info("totalUser:{}",totalUser);
        UserCollectVO userCollectVO = reportMapper.UserNewCollect(LocalDate.now().minusDays(1L));
        log.info("userCollectVO:{}",userCollectVO);
        if (userCollectVO == null){
            userCollectVO = new UserCollectVO(LocalDate.now(),0,0);
        }
        userCollectVO.setTotalUser(totalUser);
        reportMapper.insertUserCollect(userCollectVO);
        log.info("任务结束");
    }
}
